#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#include "zombies.h"
#include "level.h"

// level
level_t level;

// level map
unsigned char *mapdata;
unsigned int mapsize;

const char *levelname[]=
{
    "Day of the Tentacle",
    "Zombie Panic",
    "Evening of the Undead",
    "Terror in Aisle Five",
    "Chainsaw Hedgemaze Mayhem",
    "Weird Kids on the Block",
    "Pyramid of Fear",
    "Dr. Tongue's Castle of Terror",
    "Titanic Toddler",
    "Toxic Terrors",
    "No Assembly Required",
    "Weeds Gone Bad",
    "Mars Needs Cheerleaders",
    "Chopping Mall",
    "Seven Meals for Seven Zombies",
    "Dinner on Monster Island",
    "Ants",
    "Office of the Doomed",
    "Squidmen of the Deep",
    "Nightmare on Terror Street",
    "Invasion of the Snakeoids",
    "The Day the Earth Ran Away",
    "Revenge of Dr. Tongue",
    "The Caves of Mystery",
    "Warehouse of the Evil Dolls",
    "Look Who's Shopping",
    "Where the Red Fern Growls",
    "Dances With Werewolves",
    "Mark of the Vampire",
    "Zombie House Party",
    "The Horror of Floor Thirteen",
    "Look Who's Coming to Dinner",
    "Giant Ant Farm",
    "Fish and Crypts",
    "I Was a Chainsaw Maniac",
    "Boardwalk of Terrors",
    "Monster Phobia",
    "Labyrinth of Horrors",
    "Monsters of the Blue Lagoon",
    "Destroy All Vampires",
    "Pyramid of Fear Two",
    "Martians Go Home!",
    "Spikes",
    "Super Fund Cleanup Site",
    "The Curse of Dr. Tongue",
    "Danger in Picnic Park",
    "Day of the Chainsaw",
    "Gridiron Terror",
    "Curse of the Tongue", // 48
    "Credits: Monsters Among Us",
    "Bonus: The Son of Dr. Tongue",
    "Bonus: Day of the Tentacle",
    "Bonus: Someplace Very Warm",
    "Bonus: Curse of the Pharaohs",
    "Bonus: Mushroom Men",
    "Bonus: Cheerleaders Versus the Monsters"
};

char *getlevelname()
{
    return (char *)levelname[st.currentlevel];
}

void readmap()
{
    unsigned int mapoffset;

    mapsize=(level.width*level.height)*2;
    mapoffset=ROMToFile(level.mapoffset);
    mapdata=(unsigned char *)malloc(mapsize);

    memcpy(mapdata,romdata+mapoffset,mapsize);

    if(1)
    {
        int i;

        printf("Map layout looks like this:\n");
        for(i=0;i<mapsize;i+=2)
        {
            if(i%level.width==0) printf("\n");
            printf("%02x ",mapdata[i]);
        }
        printf("\n");
    }
}

unsigned int mapoffset(int n)
{
	unsigned int offset;

    // cast as short pointer, data is 16 bit aligned
    offset=*(unsigned short *)(romdata+0xf8202+(n<<1));
    offset+=BASE_PTR;

    return offset;
}

void readlevel(int n)
{
    unsigned int offset;

    printf("Level Name: %s\n",getlevelname(n));

    offset=mapoffset(n);
    memcpy(&level,romdata+offset,sizeof(level_t));
}
